﻿using System.Collections.Generic;
using System.IO;
//using CabLib;
using Microsoft.Deployment.Compression;
using Microsoft.Deployment.Compression.Cab;

namespace ServBus.ListForm2010.Utility
{
    public class XsnExtractor
    {
        private byte[] _contents;

        public XsnExtractor(byte[] xsnContent)
        {
            _contents = xsnContent;
        }

        public byte[] Extract(string filename)
        {
            byte[] result = null;
            //Extract ext = new Extract();
            //using (MemoryStream xsnStream = new MemoryStream(_contents))
            //{
            //    ext.SetSingleFile(filename);
            //    ext.evAfterCopyFile += (string s_File, byte[] u8_ExtractMem) =>
            //    {
            //        result = u8_ExtractMem;
            //    };
            //    ext.ExtractStream(xsnStream, "MEMORY");
            //}
            using (CabEngine engine = new CabEngine())
            {
                using (MemoryStream xsnStream = new MemoryStream(_contents))
                {
                    using (MemoryStream resultStream = engine.Unpack(xsnStream, filename) as MemoryStream)
                    {
                        result = resultStream.GetBuffer();
                    }
                }
            }
            return result;
        }

        public Dictionary<string, byte[]> Extract(List<string> filenames)
        {
            Dictionary<string, byte[]> result = new Dictionary<string, byte[]>();
            foreach (string filename in filenames)
            {
                result.Add(filename.ToLower(), null);
            }

            //Extract ext = new Extract();
            //using (MemoryStream xsnStream = new MemoryStream(_contents))
            //{
            //    ext.evAfterCopyFile += (string s_File, byte[] u8_ExtractMem) =>
            //    {
            //        string filename = Path.GetFileName(s_File).ToLower();
            //        if (result.ContainsKey(filename))
            //            result[filename] = u8_ExtractMem;
            //    };
            //    ext.ExtractStream(xsnStream, "MEMORY");
            //}
            using (CabEngine engine = new CabEngine())
            {
                using (MemoryStream xsnStream = new MemoryStream(_contents))
                {
                    foreach (ArchiveFileInfo fileInfo in engine.GetFileInfo(xsnStream))
                    {
                        string filename = Path.GetFileName(fileInfo.FullName).ToLower();
                        if (result.ContainsKey(filename))
                        {
                            using (MemoryStream resultStream = engine.Unpack(xsnStream, fileInfo.FullName) as MemoryStream)
                            {
                                result[filename] = resultStream.GetBuffer();
                            }
                        }
                    }
                }
            }
            return result;
        }
    }
}
